package org.cn.osiris.pet.service.impl;

import org.aspectj.weaver.loadtime.Aj;
import org.cn.osiris.basic.service.IBaiduAiAuditService;
import org.cn.osiris.basic.service.impl.BaiduAiAuditServiceImpl;
import org.cn.osiris.basic.util.*;
import org.cn.osiris.org.domain.Employee;
import org.cn.osiris.org.domain.Shop;
import org.cn.osiris.org.mapper.EmployeeMapper;
import org.cn.osiris.org.mapper.ShopMapper;
import org.cn.osiris.pet.domain.SearchMasterMsg;
import org.cn.osiris.pet.domain.SearchMasterMsgAuditLog;
import org.cn.osiris.pet.mapper.SearchMasterMsgAuditLogMapper;
import org.cn.osiris.pet.mapper.SearchMasterMsgMapper;
import org.cn.osiris.pet.service.ISearchMasterMsgService;
import org.cn.osiris.basic.service.impl.BaseServiceImpl;
import org.cn.osiris.user.domain.Logininfo;
import org.cn.osiris.user.domain.User;
import org.cn.osiris.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public AjaxResult publish(SearchMasterMsg searchMasterMsg, HttpServletRequest requst) {

        /*
        * 一、校验忽略
        * 二、先保存寻主消息
        * 三、百度AI审核
        *   审核通过
        *       修改状态
        *       保存日志
        *
        *
        * 四、
        *
        * */

        //首先获取登陆人id
        //获取登录信息对象
        Logininfo logininfo = LoginContext.getLogininfo(requst);
        System.out.println(logininfo);
        //通过查找登录信息中的id找到登录对象
        User user = userMapper.findByLogininfoId(logininfo.getId());

        System.out.println(user.getId());

        //查询是否已经存在数据库
        //        SearchMasterMsg searchMasterMsgId = searchMasterMsgMapper.findById(searchMasterMsg.getShopId());
        //        if (searchMasterMsgId ==null){
        //            return new AjaxResult().setSuccess(false).setMessage("请不要重复发布");
        //        }else {
        //
        //        }

        searchMasterMsg.setUserId(user.getId());
        System.out.println(searchMasterMsg);
        //保存寻主消息对象、设置自增长id
        searchMasterMsgMapper.add(searchMasterMsg);

        //然后百度AI智能审核
        //审核文字
        Boolean textAudit = baiduAiAuditService.textAudit(searchMasterMsg.getName() + searchMasterMsg.getTitle() + searchMasterMsg.getCoatColor());
        //图片审核
        Boolean imageAudit = baiduAiAuditService.imageAudit(searchMasterMsg.getResources());

        //日志
        SearchMasterMsgAuditLog searchMasterMsgAuditLog = new SearchMasterMsgAuditLog();
        //如果文本和图片审核都通过
        if (textAudit&& imageAudit){
            //审核通过 修改状态
            searchMasterMsg.setState(1);

            //保存日志
            //状态
            searchMasterMsgAuditLog.setState(1);
            //日志记录
            searchMasterMsgAuditLog.setNote("审核通过");

            //审核通过后分配就近店铺
            //获取就近店铺信息
            List<Shop> allShop = shopMapper.findAll();
            //传入发布者位置作为起点 用来判断附近周围的店铺
            Point point = DistanceUtils.getPoint(searchMasterMsg.getAddress());

            Shop nearestShop = DistanceUtils.getNearestShop(point, allShop);

            //判断发布者附近是否存在店铺 不存在就放入寻主池
            if (nearestShop ==null){
                //放入寻主池 修改状态 3 【3--寻主池】
                searchMasterMsg.setState(3);
            }else {
                //如果有 就给寻主对象添加店铺id
                searchMasterMsg.setShopId(nearestShop.getId());
                //斌且给店铺管理员发送邮件或短信提醒管理员
                Employee employee = employeeMapper.findById(nearestShop.getAdminId());
                //SmsUtils.sendSms(employee.getPhone(),"你有新的订单，请去"+searchMasterMsg.getAddress()+"接回宠物!!!")
                System.out.println("你有新的订单，请去"+searchMasterMsg.getAddress()+"接回宠物!!!");
            }

            //保存到数据库中
            searchMasterMsgAuditLogMapper.add(searchMasterMsgAuditLog);

        }else {
            //审核失败
            //修改状态
            searchMasterMsg.setState(-1);

            //保存日志
            searchMasterMsgAuditLog.setState(0);
            searchMasterMsgAuditLog.setNote("审核失败");
            searchMasterMsgAuditLogMapper.add(searchMasterMsgAuditLog);

            //删除审核失败的对象
            searchMasterMsgMapper.delete(searchMasterMsg.getId());
            //c.给用户发短信或邮件：告诉它发布失败，请重新修改信息重新发布
            //SmsUtils.sendSms(user.getPhone(),"发布失败，请重新修改信息重新发布");
            System.out.println("发布失败，请重新修改信息重新发布");
            return AjaxResult.me().setSuccess(false).setMessage("寻主消息发布失败");
        }

        return new AjaxResult();
    }
}
